package com.wuliupai.delivery.wuliupaidelivery.bean;

import android.content.Context;
import android.content.pm.PackageManager;

import com.wuliupai.delivery.wuliupaidelivery.constants.CommConstants;
import com.wuliupai.delivery.wuliupaidelivery.utils.CommonUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * Descriptions：实体类的基类
 * <p>
 * Author：ChenME
 * Date：11/14/2016
 * Email：ibelieve1210@163.com
 */
public class BaseBean implements Serializable, Cloneable {


    String version;//版本号
    String osCode;//系统标识（1、Android；2、iOS；3、pc；4、win）
    String token;
    String userId;
    String loginName;
    String userType;//用户类型(2:派送员，4司机)
    public static String UserType = "2";

    public BaseBean(Context context) {
        BaseBean object = (BaseBean) restoreObject(CommConstants.CACHE_DIR + CommConstants.TAG);
        try {
            this.version = (context.getPackageManager().getPackageInfo(context.getPackageName(), 0)).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        this.osCode = "1";
        this.userType = UserType;
        if (object == null) {
            this.token = "";
            this.userId = "";
            this.loginName = "";
        } else {
            this.token = object.getToken();
            this.userId = object.getUserId();
            this.loginName = object.getLoginName();
        }
    }

    public BaseBean() {
        BaseBean object = (BaseBean) restoreObject(CommConstants.CACHE_DIR + CommConstants.TAG);
        this.osCode = "1";
        this.version = CommonUtils.getVersionName()[0];
        this.userType = UserType;
        if (object == null) {
            this.token = "";
            this.userId = "";
            this.loginName = "";
        } else {
            this.token = object.getToken();
            this.userId = object.getUserId();
            this.loginName = object.getLoginName();
        }
    }

    public void save() {
        saveObject(CommConstants.CACHE_DIR + CommConstants.TAG, this);
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getOsCode() {
        return osCode;
    }

    public void setOsCode(String osCode) {
        this.osCode = osCode;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    /**
     * 清空用户信息
     */
    public void empty() {
        userId = null;
        token = null;
        loginName = null;
    }

    /**
     * @param path
     * @return
     */
    public final Object restoreObject(String path) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        Object object = null;

        File f = new File(path);
        boolean f1 = f.exists();
        if (!f1) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            fis = new FileInputStream(f);
            ois = new ObjectInputStream(fis);
            object = ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return object;
    }

    public static final void saveObject(String path, Object saveObject) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        File f = new File(path);
        try {
            fos = new FileOutputStream(f);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(saveObject);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}